package kr.hkit.powerjava.work.serviceui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.SQLException;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.powerjava.work.dao.DaoInterface;
import kr.hkit.powerjava.work.dto.Product;
import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.myframework.uicomp.Emp_InputPanel;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Panel_CoffeeUI extends JPanel implements ActionListener {

	private DaoInterface<Product> dao;

	private JPanel MainPanel;
	private InputPanel ProCode;
	private InputPanel ProName;
	private InputPanel ProPrice;
	private InputPanel ProCount;
	private InputPanel ProMargin;
	private JButton btnPrn2;
	private JButton btnPrn1;
	private JButton btnAdd;
	private JPanel panel;

	public Panel_CoffeeUI(DaoInterface<Product> dao) {
		super();
		this.dao = dao;
		initialize();
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));

		MainPanel = new JPanel();
		add(MainPanel, BorderLayout.CENTER);
		MainPanel.setLayout(new GridLayout(0, 1, 0, 0));

		ProCode = new InputPanel();
		MainPanel.add(ProCode);
		ProCode.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);

				String str = ProCode.getTf().getText().trim();
				Product p = new Product();
				p.setProCode(str);

				try {
					p = dao.searchItem(p);
					ProName.setTfTxt(p.getProName());
					ProPrice.setTfTxt(p.getProPrice() + "");
					ProCount.setTfTxt(p.getProCount() + "");
					ProMargin.setTfTxt(p.getProMargin() + "");
				} catch (SQLException e1) {
					e1.printStackTrace();
				} catch (NullPointerException e1) {
					if (!btnAdd.getText().equals("추가")) {
						JOptionPane.showMessageDialog(Panel_CoffeeUI.this,
								"해당 코드가 없습니다.");
					} else {
						JOptionPane.showMessageDialog(Panel_CoffeeUI.this,
								"추가 가능");
					}
				}
			}

		});
		ProName = new InputPanel();
		MainPanel.add(ProName);

		ProPrice = new InputPanel();
		MainPanel.add(ProPrice);

		ProCount = new InputPanel();
		MainPanel.add(ProCount);

		ProMargin = new InputPanel();
		MainPanel.add(ProMargin);

		panel = new JPanel();
		add(panel, BorderLayout.SOUTH);

		btnAdd = new JButton("입력");
		btnAdd.addActionListener(this);
		panel.add(btnAdd);

		btnPrn1 = new JButton("출력1");
		btnPrn1.addActionListener(this);
		panel.add(btnPrn1);

		btnPrn2 = new JButton("출력2");
		btnPrn2.addActionListener(this);
		panel.add(btnPrn2);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnPrn2) {
			actionPerformedBtnPrn2(e);
		}
		if (e.getSource() == btnPrn1) {
			actionPerformedBtnPrn1(e);
		}
		if (e.getSource() == btnAdd) {
			actionPerformedBtnAdd(e);
		}
	}
	protected void actionPerformedBtnAdd(ActionEvent e) {
		Product p = getProduct();
		try {
			dao.insertItem(p);
			JOptionPane.showMessageDialog(this, "입력되었습니다");
			panelClear();
		} catch (SQLException e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
		}
		
	
	}
	private void panelClear() {
		ProCode.setTfTxt("");
		ProName.setTfTxt("");
		ProPrice.setTfTxt("");
		ProCount.setTfTxt("");
		ProMargin.setTfTxt("");
	}

	private Product getProduct() {
		// TODO Auto-generated method stub
		return null;
	}

	protected void actionPerformedBtnPrn1(ActionEvent e) {
	}
	protected void actionPerformedBtnPrn2(ActionEvent e) {
	}
}
